#include "Client.h"

void MainMenu::Background(float* rx, float* rz){
	geMatrixMode(GE_MATRIX_PROJECTION);
	geLoadIdentity();
	gePerspective(75.0, (float)geGetContext()->width/(float)geGetContext()->height, 0.1, 100000.0);
	geMatrixMode(GE_MATRIX_VIEW);
	geLoadIdentity();
	geLookAt(0.0, 0.0, 0.0, 1.0, 0.0, 0.3-0.3*geCos(*rx));
	geRotate(0.0, 0.0, *rz);
	geSceneDraw(menu);
	*rx += 0.005;
	*rz -= 0.005;
}

MainMenu::MainMenu(Client* cli){
	client = cli;
	ticks = geGetTick();

	menu = geLoadScene("resources/models/menu.ge3d");

	geTextureWrap(menu->renderers[0].objs[0]->material.textures[0], GE_CLAMP_TO_EDGE, GE_CLAMP_TO_EDGE);
	geTextureWrap(menu->renderers[0].objs[1]->material.textures[0], GE_CLAMP_TO_EDGE, GE_CLAMP_TO_EDGE);
	geTextureWrap(menu->renderers[0].objs[2]->material.textures[0], GE_CLAMP_TO_EDGE, GE_CLAMP_TO_EDGE);
	geTextureWrap(menu->renderers[0].objs[3]->material.textures[0], GE_CLAMP_TO_EDGE, GE_CLAMP_TO_EDGE);
	geTextureWrap(menu->renderers[0].objs[4]->material.textures[0], GE_CLAMP_TO_EDGE, GE_CLAMP_TO_EDGE);
	geTextureWrap(menu->renderers[0].objs[5]->material.textures[0], GE_CLAMP_TO_EDGE, GE_CLAMP_TO_EDGE);
	geObjectMatrixIdentity(menu->renderers[0].objs[0]);
	geObjectMatrixIdentity(menu->renderers[0].objs[1]);
	geObjectMatrixIdentity(menu->renderers[0].objs[2]);
	geObjectMatrixIdentity(menu->renderers[0].objs[3]);
	geObjectMatrixIdentity(menu->renderers[0].objs[4]);
	geObjectMatrixIdentity(menu->renderers[0].objs[5]);

	bigfont = (ge_Font*)geMalloc(sizeof(ge_Font));
	memcpy(bigfont, client->res.font, sizeof(ge_Font));
	bigfont->texture = NULL;
	geFontSize(bigfont, 80);
	title = geFontMakeImage(bigfont, "MeinKraft!", RGBA(255, 255, 0, 255));

	win_main = geGuiCreateWindow("", -1, -1, GE_WINDOW_FULLSCREEN | GE_WINDOW_FULLWINDOW | GE_WINDOW_TRANSPARENT);
	geGuiStyleFont(win_main->style, client->res.font, 20);
	b_solo = geGuiCreateButton("Solo", 350, 45);
	b_multi = geGuiCreateButton("Multiplayer", 350, 45);
	b_options = geGuiCreateButton("Options", 350, 45);
	geGuiWindowLinkObject(win_main, -b_solo->width/2, -45*1.5, b_solo, GE_GUI_ALIGNX_CENTER | GE_GUI_ALIGNY_CENTER);
	geGuiWindowLinkObject(win_main, -b_multi->width/2, -45*1.5+55, b_multi, GE_GUI_ALIGNX_CENTER | GE_GUI_ALIGNY_CENTER);
	geGuiWindowLinkObject(win_main, -b_options->width/2, -45*1.5+110, b_options, GE_GUI_ALIGNX_CENTER | GE_GUI_ALIGNY_CENTER);
	
	win_multi = geGuiCreateWindow("", -1, -1, GE_WINDOW_FULLSCREEN | GE_WINDOW_FULLWINDOW | GE_WINDOW_TRANSPARENT);
	geGuiStyleFont(win_multi->style, client->res.font, 20);
	win_multi->visible = false;
	b_join = geGuiCreateButton("Join Server", 225, 40);
	b_add = geGuiCreateButton("Add Server", 225, 40);
	b_refresh = geGuiCreateButton("Refresh", 225, 40);
	b_edit = geGuiCreateButton("Edit Server", 225, 40);
	b_remove = geGuiCreateButton("Remove Server", 225, 40);
	b_cancel = geGuiCreateButton("Cancel", 225, 40);
	list = geGuiCreateListWidget(geGetContext()->width-80, geGetContext()->height-140);
	geGuiWidgetSetName(b_join, "join");
	geGuiWidgetSetName(b_add, "add");
	geGuiWidgetSetName(b_refresh, "refresh");
	geGuiWidgetSetName(b_edit, "edit");
	geGuiWidgetSetName(b_remove, "remove");
	geGuiWidgetSetName(b_cancel, "cancel");
	geGuiWidgetSetName(list, "list");
	list->back_color = RGBA(0, 0, 0, 128);
	geGuiWindowLinkObject(win_multi, -225*1.525, -95, b_join, GE_GUI_ALIGNX_CENTER | GE_GUI_ALIGNY_BOTTOM);
	geGuiWindowLinkObject(win_multi, -225/2, -95, b_add, GE_GUI_ALIGNX_CENTER | GE_GUI_ALIGNY_BOTTOM);
	geGuiWindowLinkObject(win_multi, +225*0.525, -95, b_refresh, GE_GUI_ALIGNX_CENTER | GE_GUI_ALIGNY_BOTTOM);
	geGuiWindowLinkObject(win_multi, -225*1.525, -50, b_edit, GE_GUI_ALIGNX_CENTER | GE_GUI_ALIGNY_BOTTOM);
	geGuiWindowLinkObject(win_multi, -225/2, -50, b_remove, GE_GUI_ALIGNX_CENTER | GE_GUI_ALIGNY_BOTTOM);
	geGuiWindowLinkObject(win_multi, +225*0.525, -50, b_cancel, GE_GUI_ALIGNX_CENTER | GE_GUI_ALIGNY_BOTTOM);
	geGuiWindowLinkObject(win_multi, 40, 30, list, 0);

	win_options = geGuiCreateWindow("", -1, -1, GE_WINDOW_FULLSCREEN | GE_WINDOW_FULLWINDOW | GE_WINDOW_TRANSPARENT);
	win_options->style = win_multi->style;
	win_options->visible = false;
	
	int mx, my;
	float tw = title->width * 1.2;
	float th = title->height * 1.2;
	float twd = 0.5;
	float thd = -0.5;
	rx = 0.0;
	rz = 0.0;
	while(1){
		geReadKeys(client->keys);
		if(geKeysToggled(client->keys, GEK_F11)){
			if(client->options.fullscreen){
				geFullscreen(false, -1, -1);
				client->options.fullscreen = false;
			}else{
				geFullscreen(true, -1, -1);
				client->options.fullscreen = true;
			}
		}
		geCursorPosition(&mx, &my);

		if(b_solo->pressed){
			win_main->visible = false;
			client->Solo();
			win_main->visible = true;
		}
		if(b_multi->pressed){
			win_main->visible = false;
			win_multi->visible = true;
			//MultiplayerMenu();
			win_multi->visible = false;
			client->Multiplayer();
			win_main->visible = true;
		}
		
		geDrawingMode(GE_DRAWING_MODE_3D);
		geClearScreen();
		Background(&rx, &rz);
		geDrawingMode(GE_DRAWING_MODE_2D);

		geDrawImageStretched(geGetContext()->width/2-tw/2, 100-th/2, title, (int)tw, (int)th);
		tw += twd;
		th += thd;
		if(tw > (title->width*1.25) || th > (title->height*2.0)){
			twd = -twd;
			thd = -thd;
		}

		geSwapBuffers();
		ticks = geWaitTick((int)1000/60, ticks);
	}
}

MainMenu::~MainMenu(){
}

void MainMenu::MultiplayerMenu(){
	while(1){
		geReadKeys(client->keys);
		if(geKeysToggled(client->keys, GEK_F11)){
			if(client->options.fullscreen){
				geFullscreen(false, -1, -1);
				client->options.fullscreen = false;
			}else{
				geFullscreen(true, -1, -1);
				client->options.fullscreen = true;
			}
		}
		
		geDrawingMode(GE_DRAWING_MODE_3D);
		geClearScreen();
		Background(&rx, &rz);
		geDrawingMode(GE_DRAWING_MODE_2D);

		geSwapBuffers();
		ticks = geWaitTick((int)1000/60, ticks);
	}
}
